/*
 * @Description: 所有页面的路由配置
 * @Author: wangwangwang
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-08-18 09:55:46
 */

import { getButtonList } from "@/api/common.js";
import { getPerMission, getUserInfo, setButtonList } from "@/utils/auth";
import Watermark from '@/utils/watermark';
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import Vue from "vue";
import VueRouter from "vue-router";
import { ICPS } from "./ICPS/index.js";

Vue.use(VueRouter);
/* *初始路由
 * keepAlive为缓存组件，默认为全局缓存，设置为true为不缓存
 * 
*/
export const routes = [
  {
    path: "/", //这个表示的是根目录，即一进入的页面
    redirect: "/lvshe/index", //设置页面一进来就显示的页面，即重定向到goods组件，这里写的内容是对应组将的component的值
  },
  {
    path: '/wps',
    name: 'wps',
    component: () => import('@/views/wps.vue'),
    meta: {
      keepAlive: true
    },
  },
  {
    path: '/map',
    name: 'map',
    component: () => import('@/views/map.vue'),
  },
  {
    path: "/login",
    name: "login",
    // redirect: '/login',
    component: () => import("@/views/Login.vue"),
  },
  {
    path: "/indexEditor",
    name: "indexEditor",
    component: () => import("@/views/home/indexEditor/indexEditor.vue"),
    meta: {
      title: "首页编辑",
      keepAlive: true
    },
  },
  {
    path: "/indexSee",
    name: "indexSee",
    component: () => import("@/views/home/indexSee/indexSee.vue"),
    meta: {
      title: "首页预览",
      keepAlive: true
    },
  },
  {
    path: "/lvshe",
    component: () => import("@/views/pages/pages.vue"),
    children: [{
      path: "index",
      name: "index",
      component: () => import(/* webpackChunkName: 'home'*/"@/views/home/index/index.vue"),
      meta: {
        title: "首页",
        keepAlive: true
      },
    },
    {
      path: "details",
      name: "details",
      component: () => import(/* webpackChunkName: 'home'*/"@/views/pages/details.vue"),
      meta: {
        title: "通知详情",
      },
    },
    {
      path: "Saleskanban",
      name: "Saleskanban",
      component: () => import(/* webpackChunkName: 'home' */"@/views/pages/Saleskanban/Saleskanban.vue"),
      meta: {
        title: "销售看板",
        keepAlive: true,
      },
    },
    {
      path: "userInfo",
      name: "userInfo",
      component: () => import(/* webpackChunkName: 'home'*/"@/views/pages/userInfo/userInfo.vue"),
      meta: {
        title: "个人中心",
        keepAlive: true
      },
    },
    {
      path: "download",
      name: "download",
      component: () => import(/* webpackChunkName: 'home'*/"@/views/pages/download/download.vue"),
      meta: {
        title: "下载专区",
        keepAlive: true
      },
    },
    {
      path: "controlLibrary",
      name: "controlLibrary",
      component: () =>
        import(/* webpackChunkName: 'home'*/
          "@/views/platform/platform_item/Signed/processConfig/controlLibrary.vue"
        ),
      meta: {
        title: "控件库配置",
      }
    },
    {
      path: "base",
      component: () => import("@/views/platform/platform.vue"),
      children: [
        {
          path: "basicIndex",
          name: "basicIndex",
          component: () =>
            import(/* webpackChunkName: 'huanyin'*/
              "@/views/platform/platform_item/index.vue"
            ),
          meta: {
            title: "欢迎使用基础系统",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "organization",
          name: "organization",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/organization/organization.vue"
            ),
          meta: {
            title: "组织管理",
            system: "organization",
            // keepAlive: true
          },
        },
        {
          path: "jobs",
          name: "jobs",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/"@/views/platform/platform_item/basicData/jobs/jobs.vue"),
          meta: {
            title: "岗位管理",
            system: "organization",
            keepAlive: true
          },
        },
        {
          path: "user",
          name: "user",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/"@/views/platform/platform_item/basicData/user/user.vue"),
          meta: {
            title: "用户管理",
            system: "organization",
            // keepAlive: true,
          },
        },
        {
          path: "business",
          name: "business",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/business/business.vue"
            ),
          meta: {
            title: "业务系统管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "function",
          name: "function",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/function/function.vue"
            ),
          meta: {
            title: "功能管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "IndividualUser",
          name: "IndividualUser",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/IndividualUser/IndividualUser.vue"
            ),
          meta: {
            title: "个人用户创建",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "PersonalRights",
          name: "PersonalRights",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/PersonalRights/PersonalRights.vue"
            ),
          meta: {
            title: "个人权限授权",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "permissions",
          name: "permissions",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/permissions/permissions.vue"
            ),
          meta: {
            title: "权限管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "PermissionsApply",
          name: "PermissionsApply",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/PermissionsApply/PermissionsApply.vue"
            ),
          meta: {
            title: "权限申请",
            system: "organization",
          },
        },
        {
          path: "partTime",
          name: "partTime",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/partTime/partTime.vue"
            ),
          meta: {
            title: "兼职管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "role",
          name: "role",
          component: () =>
            import("@/views/platform/platform_item/basicData/role/role.vue"),
          meta: {/* webpackChunkName: 'basicData1'*/
            title: "角色管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "DataAccess",
          name: "DataAccess",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/DataAccess/DataAccess.vue"
            ),
          meta: {
            title: "数据权限配置",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "dictionary",
          name: "dictionary",
          component: () =>
            import(/* webpackChunkName: 'basicData1'*/
              "@/views/platform/platform_item/basicData/dictionary/dictionary.vue"
            ),
          meta: {
            title: "字典管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "notice",
          name: "notice",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/notice/notice.vue"
            ),
          meta: {
            title: "通知通告",
            system: "organization",
            // keepAlive: true,
          },
        },
        {
          path: "noticeEdit",
          name: "noticeEdit",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/notice/noticeEdit.vue"
            ),
          meta: {
            title: "编辑通知通告",
            system: "organization",
          },
        },
        {
          path: "pageConfig",
          name: "pageConfig",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/pageConfig/pageConfig.vue"
            ),
          meta: {
            title: "首页配置",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "scheduling",
          name: "scheduling",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/scheduling/scheduling.vue"
            ),
          meta: {
            title: "调度维护",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "APPmanagement",
          name: "APPmanagement",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/APPmanagement/APPmanagement.vue"
            ),
          meta: {
            title: "APP管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "mobileBanner",
          name: "mobileBanner",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/mobileBanner/mobileBanner.vue"
            ),
          meta: {
            title: "移动端banner",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "aboutUs",
          name: "aboutUs",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/aboutUs/aboutUs.vue"
            ),
          meta: {
            title: "关于华西",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "attachment",
          name: "attachment",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/attachment/attachment"
            ),
          meta: {
            title: "附件管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "CodingRules",
          name: "CodingRules",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/CodingRules/CodingRules"
            ),
          meta: {
            title: "单据编码规则",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "SystemParameters",
          name: "SystemParameters",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/SystemParameters/SystemParameters"
            ),
          meta: {
            title: "系统参数",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "KeytoLog",
          name: "KeytoLog",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/KeytoLog/KeytoLog"
            ),
          meta: {
            title: "关键日志记录",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "DailyLog",
          name: "DailyLog",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/DailyLog/DailyLog"
            ),
          meta: {
            title: "日常日志记录",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "InterfaceTime",
          name: "InterfaceTime",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/InterfaceTime/InterfaceTime"
            ),
          meta: {
            title: "接口访问时长",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "MessageLog",
          name: "MessageLog",
          component: () =>
            import(/* webpackChunkName: 'basicData2'*/
              "@/views/platform/platform_item/basicData/MessageLog/MessageLog"
            ),
          meta: {
            title: "消息日志",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "DieEemente",
          name: "DieEemente",
          component: () =>
            import(/* webpackChunkName: 'Signed'*/
              "@/views/platform/platform_item/Signed/DieEemente/DieEemente.vue"
            ),
          meta: {
            title: "审批要素",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "TypeApprover",
          name: "TypeApprover",
          component: () =>
            import(/* webpackChunkName: 'Signed'*/
              "@/views/platform/platform_item/Signed/TypeApprover/TypeApprover.vue"
            ),
          meta: {
            title: "审批人类型",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "processAgent",
          name: "processAgent",
          component: () =>
            import(/* webpackChunkName: 'Signed'*/
              "@/views/platform/platform_item/Signed/processAgent/processAgent.vue"
            ),
          meta: {
            title: "流程代理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "ProcessManagement",
          name: "ProcessManagement",
          component: () =>
            import(/* webpackChunkName: 'Signed'*/
              "@/views/platform/platform_item/Signed/ProcessManagement/ProcessManagement.vue"
            ),
          meta: {
            title: "流程管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "ProcessTaskMaintenance",
          name: "ProcessTaskMaintenance",
          component: () =>
            import(/* webpackChunkName: 'Signed'*/
              "@/views/platform/platform_item/Signed/ProcessTaskMaintenance/ProcessTaskMaintenance.vue"
            ),
          meta: {
            title: "流程任务维护",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "processConfig",
          name: "processConfig",
          component: () =>
            import(/* webpackChunkName: 'Signed'*/
              "@/views/platform/platform_item/Signed/processConfig/processConfig.vue"
            ),
          meta: {
            title: "流程配置",
            system: "organization",
          },
          hidden: true,
        },
        {
          path: "NewTask",
          name: "NewTask",
          component: () =>
            import(/* webpackChunkName: 'Signed'*/"@/views/platform/platform_item/task/NewTask/NewTask.vue"),
          meta: {
            title: "新建任务",
            system: "organization",
            // keepAlive: true,
          },
        },
        {
          path: "taskCenter",
          name: "taskCenter",
          component: () =>
            import(/* webpackChunkName: 'task'*/
              "@/views/platform/platform_item/task/taskCenter/taskCenter.vue"
            ),
          meta: {
            title: "新建任务",
            system: "organization",
            // keepAlive: true,
          },
        },
        {
          path: "taskDetails",
          name: "taskDetails",
          component: () =>
            import(/* webpackChunkName: 'task'*/
              "@/views/platform/platform_item/task/taskCenter/taskDetails.vue"
            ),
          meta: {
            title: "任务处理",
            system: "organization",
            // keepAlive: true
          },
        },
        {
          path: "taskagents",
          name: "taskagents",
          component: () =>
            import(/* webpackChunkName: 'task'*/
              "@/views/platform/platform_item/task/taskagents/taskagents"
            ),
          meta: {
            title: "待办任务",
            system: "organization",
          },
        },
        {
          path: "undertakesTask",
          name: "undertakesTask",
          component: () =>
            import(/* webpackChunkName: 'task'*/
              "@/views/platform/platform_item/task/undertakesTask/undertakesTask"
            ),
          meta: {
            title: "待发任务",
            system: "organization",
          },
        },
        {
          path: "iama",
          name: "iama",
          component: () =>
            import(/* webpackChunkName: 'task'*/"@/views/platform/platform_item/task/iama/iama"),
          meta: {
            title: "本人发起",
            system: "organization",
          },
        },
        {
          path: "submiTask",
          name: "submiTask",
          component: () =>
            import(/* webpackChunkName: 'task'*/"@/views/platform/platform_item/task/submiTask/submiTask"),
          meta: {
            title: "提交任务",
            system: "organization",
          },
        },
        {
          path: "doTasks",
          name: "doTasks",
          component: () =>
            import(/* webpackChunkName: 'task'*/"@/views/platform/platform_item/task/doTasks/doTasks"),
          meta: {
            title: "已办任务",
            system: "organization",
          },
        },
        {
          path: "colTask",
          name: "colTask",
          component: () =>
            import(/* webpackChunkName: 'task'*/"@/views/platform/platform_item/task/colTask/colTask"),
          meta: {
            title: "收藏任务",
            system: "organization",
          },
        },
        {
          path: "Trackitems",
          name: "Trackitems",
          component: () =>
            import(/* webpackChunkName: 'task'*/
              "@/views/platform/platform_item/task/Trackitems/Trackitems"
            ),
          meta: {
            title: "跟踪事项",
            system: "organization",
          },
        },
        {
          path: "ThirdpartyApp",
          name: "ThirdpartyApp",
          component: () =>
            import(/* webpackChunkName: 'interface'*/"@/views/platform/platform_item/interface/ThirdpartyApp"),
          meta: {
            title: "第三方应用维护",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "APIReg",
          name: "APIReg",
          component: () =>
            import(/* webpackChunkName: 'interface'*/"@/views/platform/platform_item/interface/APIReg"),
          meta: {
            title: "对外API注册",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "syncDatasource",
          name: "syncDatasource",
          component: () =>
            import(/* webpackChunkName: 'sync'*/"@/views/platform/platform_item/sync/syncDatasource"),
          meta: {
            title: "数据源管理",
            system: "organization",
            keepAlive: true,
          },
        },
        {
          path: "syncTask",
          name: "syncTask",
          component: () =>
            import(/* webpackChunkName: 'sync'*/"@/views/platform/platform_item/sync/syncTask"),
          meta: {
            title: "同步任务管理",
            system: "organization",
            keepAlive: true,
          },
        },
      ],
    },
    ],
  },
];
/* 动态路由*/
export const errroutes = [{
  path: "/404",
  component: () => import("@/views/error/error404"),
},
{
  path: "/401",
  component: () => import("@/views/error/error401"),
},
{
  path: "*",
  redirect: "/401",
},
];
export const addroutes = [...ICPS, ...errroutes];
const router = new VueRouter({
  routes,
});
/* 动态添加路由*/
router.addRoutes(addroutes);
/*进度条配置*/
NProgress.configure({
  easing: "linear",
  speed: 500, // 动画时间
  showSpinner: false, // 不显示转动的小圆圈
});
export default router;

const whiteList = ['checkflows', 'board_Salesofkanban', 'checkflows2', 'EngineeringEvaluation', 'login', 'index', 'download', 'userInfo', 'Saleskanban', 'details', 'ProjectClarifiFlow', 'ChangeLevelCoop', 'SupplierRating', 'SiteProportion', 'CarTransfer', 'PurchaseOrder', 'ShareMaintenance', 'AddOrder', 'RiskControlFlow', 'ThirdCheck1', 'ThirdCheck2', 'SpringbackCheck', 'ExceptionsApply1', 'ExceptionsApply2', 'ExceptionsApply3', 'taskDetails', "SpecialCheckFlows", 'sign_mix', 'sign_CPYquality', 'sign_Sitequality', 'sign_Wise_contactsbusiness1', 'sign_Wise_contactsbusiness2', 'sign_material', 'sign_concrete', 'sign_Wise_rentunit1', 'sign_Wise_rentunit2', 'sign_Wise_rentunit3', 'sign_smartschedulj', 'sign_stocksake1', 'sign_stocksake2', 'sign_smartscheduld', 'sign_ADRN', 'basicIndex', 'ICPSIndex', 'wps', 'processConfig', 'sign_smartdata', 'smartdataPlan', 'materialScrap','indexEditor', 'indexSee', 'outUrl', 'map', 'insideUrl', 'cancleOrder'];
/**
 * 进入路由前
*/
router.afterEach(async () => {
  const hasWaterMask = window.sessionStorage.getItem('watermark') === 'Y';
  const UserInfo = await getUserInfo();
  if (UserInfo && hasWaterMask) {
    Watermark.set(UserInfo.userRealName)
  }
  NProgress.done();
});
router.beforeEach((to, from, next) => {
    
  NProgress.start();
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  /**对于有权限的页面进行按钮权限获取*/
  const token = window.localStorage.getItem('access-token') || null;
  if (token && to.path !== '/login') {
    getButtonList({ functionCode: to.name }).then(res => {
      if (res.code == 1001) {
        if (!token) {
            if (to.path !== '/login') {
              next({ path: '/login' })
            }
            else {
              next();
            }
        }
        else {
            next();
        }
        setButtonList(res.data)
        next();
      
    } else {
        next({ path: "/404" });
    }
  })
  }
  
  /**
   * @Description: 设置白名单
   * @Param: 
   * @Author: wangwangwang
   */
  if (whiteList.indexOf(to.name) !== -1) {
    if (to.name == 'board_Salesofkanban') {
      getButtonList({ functionCode: to.query.name || 'board_Salesofkanban' }).then(res => {
        if (res.code == 1001) {
          next();
          setButtonList(res.data)
        } else {
          next({ path: "/404" });
        }
      })
      return
    }
    next();
    return
  }
  /**404,401不需要权限*/
  if (to.path == "/401" || to.path == "/404") {
    next();
    return
  }
  /**对需要权限的页面进行权限验证，无权限条状401报错页面*/
  if (getPerMission()?.functionLimitList.findIndex(res => (res.code == to.name)) == -1) {
    next({ path: "/401" });
    return
  }
  // 拦截运输地图，跳单独的页面
  if (to.name === 'smartschedulk') {
    let routeUrl = router.resolve({
      path: "/map",
    });
    window.open(routeUrl.href, '_blank');
    NProgress.done();
    return
  }
});
